package com.zgkxzx.mvpbase.api;



import com.zgkxzx.mvpbase.bean.BaseRequestBean;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


/**
 * 创建者   zgkxzx
 * 创建日期 2016/11/11.
 * 功能描述 网络请求的具体接口
 */
public class ApiServiceImpl {
    private final static String TAG = ApiServiceImpl.class.getSimpleName();

    /**
     * 通用的请求设置
     *
     * @return
     */
    private static ApiService ApiServiceImpl() {
        return ApiManager.getInstance().initRetrofit().create(ApiService.class);
    }

    /**
     * 请求变换
     * @param <T>
     * @param observable 观察者
     * @return
     */
    private static <T>Observable<T> applySchedulers(Observable<T> observable){
        return  observable
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 请求转换成观察者和订阅者
     * @param observable 观察者
     * @param subscriber 订阅者
     * @param <T>
     * @return
     */
    private static <T>Subscription applySchedulers(Observable<T> observable,Subscriber<T> subscriber){
        return applySchedulers(observable).subscribe(subscriber);
    }


    /**
     * 现场控制变换
     * @param <T>
     * @return
     */
    public static <T>Observable.Transformer<T,T> applyTransformer(){
        return new Observable.Transformer<T,T>() {
            @Override
            public Observable<T> call(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }



    private static void showProgressDialog(){}


    /**
     * 上传数据到微信运动
     *
     * @param subscriber
     * @param token
     * @param param
     */
    public static void uploadWXSport(final Subscriber<BaseRequestBean<String>> subscriber, String token, String param) {
        ApiServiceImpl().uploadWXSport(token, param)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public static void uploadWxSport2(final Subscriber<BaseRequestBean<String>> subscriber, String token, String param){
        applySchedulers(ApiServiceImpl().uploadWXSport(token,param),subscriber);
    }


    public static void uploadWXSport3(final Subscriber<BaseRequestBean<String>> subscriber, String token, String param) {
        ApiServiceImpl().uploadWXSport(token, param)
                .compose(ApiServiceImpl.<BaseRequestBean<String>>applyTransformer())
                .subscribe(subscriber);
    }

    /**
     * 上传数据到微信运动2
     *
     * @param subscriber
     * @param token
     * @param param
     */
    public static void uploadWXSport2(final Subscriber<BaseRequestBean<String>> subscriber, String token, String param) {
        ApiServiceImpl().uploadWXSport2(token, param)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }



    /**
     * 通用GET请求接口
     *
     * @param subscriber
     * @param url
     */
    public static void getNetInfoByUrl(final Subscriber<String> subscriber, String url) {
        Call<ResponseBody> netInfoByUrl = ApiServiceImpl().getNetInfoByUrl(url);
        netInfoByUrl.enqueue(new Callback<ResponseBody>() {

            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (!response.isSuccessful()) {
                    return;
                }
                try {
                    subscriber.onNext(response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onNext(null);
                }
                subscriber.onCompleted();
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                subscriber.onError(t);
                subscriber.onCompleted();
            }
        });
    }


}


